import ch.qos.logback.classic.AsyncAppender
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import com.zeus.cloud.logback.filter.DynamicThresholdLoggerFilter

//define const system var . -DlogPath=d:/logs/
def ENV_LOG_PATH = "logPath"
def ENV_LOG_LAYOUT = "logLayout"
def ENV_LOG_LEVEL = "logLevel"
def ENV_LOG_NAME = "logName"
def ENV_CLOUD_CONFIG_COMPLETE = "hasComplete"

def DEFAULT_LAYOUT_TEMPLATE = "[%date{ISO8601}] [%level] %logger{80} %thread [%X{traceId}] [%contextName] - %msg%n"
def DEFAULT_ROOT_LOGGER_LEVEL = "info"
def SYSTEM_OS_NAME = "os.name"
//scan("30 seconds")
// load external file source properties
def sysProperties = System.properties
def loaded = sysProperties.get(ENV_CLOUD_CONFIG_COMPLETE,true)
def patternTemplate = sysProperties.getProperty(ENV_LOG_LAYOUT,DEFAULT_LAYOUT_TEMPLATE)
def logLevel = sysProperties.getProperty(ENV_LOG_LEVEL,DEFAULT_ROOT_LOGGER_LEVEL)
def logName = sysProperties.getProperty(ENV_LOG_NAME,"")
def osName = sysProperties.getProperty(SYSTEM_OS_NAME).toLowerCase()
def logBase = "/data/log-center/"
if (osName.contains("windows")){
    logBase = "d:/log-center/"
}
def logPath = sysProperties.getProperty(ENV_LOG_PATH,logBase)
def rootLogLevel = Level.toLevel(logLevel)
def rootLogAppender = ["STDOUT"]
appender("STDOUT", ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "${patternTemplate}"
    }
}
turboFilter(DynamicThresholdLoggerFilter)

if(loaded){
    context.name = logName
    logPath = logPath + logName
    def dir = new File(logPath)
    if(!dir.exists()){
        dir.mkdirs()
    }
    appender("FILE", RollingFileAppender) {
        file = "${logPath}/server.log"
        rollingPolicy(TimeBasedRollingPolicy) {
            maxHistory = 30
            fileNamePattern = "${logPath}/server.%d{yyyy-MM-dd}.log"
        }
        encoder(PatternLayoutEncoder) {
            pattern = "${patternTemplate}"
        }
    }
    appender("ASYNC", AsyncAppender) {
        discardingThreshold = 20
        queueSize = 2048
        includeCallerData = true
        neverBlock = true
        maxFlushTime = 10000
        appenderRef("FILE")
    }
    rootLogAppender = ["ASYNC","STDOUT"]
}
root(rootLogLevel,rootLogAppender)

logger("org.springframework", INFO, [], true)
logger("com.alibaba.dubbo", WARN, [], true)
logger("com.alibaba.boot", WARN, [], true)
logger("org.spring", WARN, [], true)
logger("org.hibernate", WARN, [], true)
logger("io.grpc.netty", OFF, [], true)
logger("org.eclipse.jetty", WARN, [], true)
logger("jndi", WARN, [], true)
logger("redis.clients", WARN, [], true)
logger("application", WARN, [], true)
logger("springfox.documentation", WARN, [], true)
logger("com.netflix", WARN, [], true)
logger("io.grpc.internal.SerializingExecutor", OFF, [], true)
logger("org.apache", WARN, [], true)
logger("io.grpc.internal.ClientCallImpl", OFF, [], true)
logger("org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration", INFO, [], true)
logger("com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter", DEBUG, [], true)
logger("de.codecentric.boot.admin.client.registration.ApplicationRegistrator", INFO, [], true)
logger("com.hengxinyongli.iam.mapper", TRACE, [], true)
